//
//  ProfileViewController.m
//  宝宝儿歌
//
//  Created by Clement_Gu on 16/4/18.
//  Copyright © 2016年 baobaoerge. All rights reserved.
//

#import "ProfileViewController.h"
#import "MyFavoriteTableViewController.h"
#import "TimerViewController.h"
#import "UIImageView+WebCache.h"
#import "ProfileInfoTableViewCell.h"
#import "DetailViewController.h"
@interface ProfileViewController ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSArray *arrList;

@end

@implementation ProfileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //创建title
    self.title = @"我";
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    //获得界面的尺寸
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat Height = [UIScreen mainScreen].bounds.size.height;
    self.arrList = @[@"我的收藏",@"设置与帮助",@"清理缓存",@"检测网络",@"定时",@"版本"];
    //创建界面
    //创建tableView
    [self buildTableView:CGRectMake(0,0, width, Height)];
//    [self.tableView registerClass:[ProfileInfoTableViewCell class] forCellReuseIdentifier:@"Cell"];
    //
    [self buildUserDefault];
//    self.profileInfo = [NSMutableDictionary dictionary];
////    self.profileInfo = @{@"userName":@"用户名",
////                         @"userSignature":@"个性签名",
////                         @"userPhoto":[UIImage imageNamed:@"placeholder"]};
//    [self.profileInfo setObject:@"用户名" forKey:@"userName"];
//    [self.profileInfo setObject:@"个性签名" forKey:@"userSignature"];
//    [self.profileInfo setObject:[UIImage imageNamed:@"placeholder"] forKey:@"userPhoto"];
    
    
}
/**
 *  userdefault 存储数据
 */
-(void)buildUserDefault
{
//    NSMutableDictionary *profileInfo = [NSMutableDictionary dictionary];
//    [profileInfo setObject:@"用户名" forKey:@"userName"];
//    [profileInfo setObject:@"个性签名" forKey:@"userSignature"];
//    [profileInfo setObject:[UIImage imageNamed:@"placeholder"] forKey:@"userPhoto"];
//    NSMutableArray *profileInfo = [NSMutableArray array];
//    UIImage *img = [UIImage imageNamed:@"placeholder"];
//    [profileInfo addObject:@"用户名"];
//    [profileInfo addObject:@"个性签名"];
//    [profileInfo addObject:@"placeholder"];
//    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
//    [def setObject:profileInfo forKey:@"myInfo"];
    
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    [def setObject:@"用户名" forKey:@"userName"];
    [def setObject:@"个性签名" forKey:@"userSignature"];
    [def setObject:@"placeholder" forKey:@"userPhoto"];

    
    
    [def synchronize];
}
//页面翻页
-(void)changePage
{
    CATransition *anima = [CATransition animation];
    anima.duration = 2;
    anima.timingFunction = UIViewAnimationCurveEaseInOut;
    anima.type = @"pageCurl";
//    anima.type =kCATransitionPush;
    anima.subtype = kCATransitionFromLeft;
    [self.view.layer addAnimation:anima forKey:@"leftin"];
    
}

-(void)addPhoto
{
    //将资源设置成相机
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    //如果相机不可用则设置成相片库
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    //初始化
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
    imagePicker.delegate = self;
    imagePicker.allowsEditing = YES;//设置可编辑
    imagePicker.sourceType = sourceType;
    [self presentViewController:imagePicker animated:YES completion:nil];

    

}

#pragma mark Build All Controls
-(void)buildTableView:(CGRect)frame
{
    //tableView 在创建之时已经决定好了 样式且不可改变 所以要在初始化的时候设置样式
    UITableView *tableView = [[UITableView alloc]initWithFrame:frame style:UITableViewStyleGrouped];
    self.tableView = tableView;
//    self.tableView.style = UITableViewStyleGrouped;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:tableView];
}



#pragma mark TableViewDelegate and DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    else
    {
        return 6;
    }
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        if (indexPath.section == 0) {
            [tableView registerClass:[ProfileInfoTableViewCell class] forCellReuseIdentifier:@"cell"];
            ProfileInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
     
            NSUserDefaults *df= [NSUserDefaults standardUserDefaults];
       
            cell.nameLabel.text = [df objectForKey:@"userName"];
            if ([[df objectForKey:@"userPhoto"] isEqualToString:@"placeholder"]) {
                  cell.userPhoto.image =[UIImage imageNamed:[df objectForKey:@"userPhoto"]];
            }
            else
            {
                cell.userPhoto.image = [UIImage imageWithData:[df objectForKey:@"userPhoto"]];
            }
            
          
            cell.SignatureLabel.text = [df objectForKey:@"userSignature"];
          
            return cell;
    }
    else
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        

        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        }
        cell.textLabel.text = self.arrList[indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
           return  cell;
        
    }
    
 
}

//设置表格的行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 80;
    }
    else
    {
        return 44;
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 60;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        switch (indexPath.row) {
            case 0:
            {
                MyFavoriteTableViewController *myFavoriteVC = [[MyFavoriteTableViewController alloc]init];
                [self.navigationController pushViewController:myFavoriteVC animated:YES];
                [self changePage];
//                [self presentViewController:myFavoriteVC animated:NO completion:nil];
            
            }
                break;
            case 1:
            {
//                MyFavoriteTableViewController *myFavoriteVC = [[MyFavoriteTableViewController alloc]init];
//                //                [self.navigationController pushViewController:myFavoriteVC animated:YES];
//                [self changePage];
//                [self presentViewController:myFavoriteVC animated:NO completion:nil];
                
            }
                break;
            case 2:
            {
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要清空数据么？清空后播放需重新下载。" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *actionDone = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    NSLog(@"执行删除操作");
                }];
                UIAlertAction *actioneCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
                [alert addAction:actionDone];
                [alert addAction:actioneCancel];
//                [self changePage];
                [self presentViewController:alert animated:YES completion:nil];
                
            }
                break;
            case 3:
            {
                NSLog(@"检查网络代码");
                
            }
                break;
            case 4:
            {
                TimerViewController *timerVC = [[TimerViewController alloc]init];
                [self changePage];
//                [self presentViewController:timerVC animated:NO completion:nil];
                [self.navigationController pushViewController:timerVC animated:YES];
                
            }
                break;
            case 5:
            {
//                MyFavoriteTableViewController *myFavoriteVC = [[MyFavoriteTableViewController alloc]init];
                //                [self.navigationController pushViewController:myFavoriteVC animated:YES];
//                [self changePage];
              
//                [self presentViewController:myFavoriteVC animated:NO completion:nil];
                
            }
                break;
                
                
            default:
                break;
        }
    }
    else
    {
        DetailViewController *detailVC = [[DetailViewController alloc]init];
//        [self changePage];
        [detailVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
        [self presentViewController:detailVC animated:YES completion:nil];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
